<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mod_users extends CI_Controller {
    private $rear = '';
    private $content='';
    function  __construct(){
        parent::__construct();
        if($this->session->userdata('ngonngu') && $this->session->userdata('ngonngu') == 'en'){
            $this->rear = '_en';
            $this->lang->load('atta','english');
        }else{
            $this->lang->load('atta','vietnam');
        }
        $this->load->library(array('session','form_validation','security',)); 
        $this->load->helper(array('form', 'text'));
        $this->load->model('Home_model');
        $this->load->model('Admin_model');
    }
    function index(){
        
        $this->form_validation->set_rules('username', 'Tên đăng  nhập', 'trim|required|min_length[5]|max_length[12]|xss_clean');
        $this->form_validation->set_rules('password', 'Mật khẩu', 'trim|required|matches[passconf]|xss_clean');
        $this->form_validation->set_rules('passconf', 'Mật khẩu xác nhận', 'trim|required|xss_clean');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|xss_clean');
        $this->form_validation->set_rules('hoten','Họ tên','required|xss_clean');           
        $this->form_validation->set_rules('diachi','Địa chỉ','required|xss_clean');           
        $this->form_validation->set_rules('phone','Điện thoại','required|numeric|max_length[12]|min_length[9]|xss_clean');
        if ($this->form_validation->run() == FALSE){
			
		}else{
		 	$data = $_POST;
            $data['last_ip']=$this->input->ip_address();
            $data['password']=sha1($data['password']);         
            unset($data['passconf']); 
            if(!$this->check_user($data['username'],$data['email'],$data['password'])){
                $this->db->insert('users',$data);
                $this->session->set_userdata('thongbao',1);
            }
            $this->session->set_userdata('trung_user',1);
            
		}
        $this->content=$this->load->view('mod_users/register','',true);
        $this->hienthi();  
    }
    public function login(){ 
	   $this->form_validation->set_rules('username', 'Tên đăng nhập', 'trim|required|xss_clean');
       $this->form_validation->set_rules('password', 'Mật khẩu', 'trim|required|xss_clean');      
       if ($this->form_validation->run() == TRUE){
            $username = $this->security->xss_clean($_POST['username']);
            $password = $this->security->xss_clean($_POST['password']);
            $password = sha1($password);
            $log = $this ->Admin_model->get_one('users',array('username'=>$username,'password'=>$password));
            $num = $this ->Admin_model->get_number('users',array('username'=>$username,'password'=>$password));
            //get_number            
            if($num==1){
               $this->session->set_userdata('logedin',1);
               $this->session->set_userdata('id',$log->id); 
               $this->session->set_userdata('username',$log->username); 
               $this->session->set_userdata('thongbaodn',1);
               if($this->session->userdata('url_cart')){
                    redirect($this->session->userdata('url_cart'));
               }             
            }else{
                $this->session->set_userdata('loidn','Tên đăng nhập hoặc mật khẩu không đúng');
            }
       }
                
      $this->content=$this->load->view('mod_users/login','',true);
      $this->hienthi(); 
	}
    function check_user($username,$email,$pass){        
        if($this->Home_model->check_trung(array('username'=>$username,'password'=>$pass),'users')||
        $this->Home_model->check_trung(array('email'=>$email,'password'=>$pass),'users') ){
            return true;
        }else{
            return false;
        }
    }
    function logout(){
        $this->session->unset_userdata('logedin');
        $this->session->unset_userdata('id'); 
        $this->session->unset_userdata('username'); 
        redirect(base_url());
    }
    function hienthi(){
        //$this->load->view('com_cart/form_cart');
        $data['content'] = $this->content;
        $data['title'] = 'tiêu đề';
        $data['metades'] = 'metadesc';
        $data['metakey'] = 'metakey';
        $this->load->view('default/index',$data); 
    }
    
}